// pages/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      "id": 0,
      "img": "",
      "name": "",
      "leader": 0,
    },
    gameInfo: [],
    userIds: [],
    showModalStatus: false,
    number: 1,
    number1: 4,
    disabled1: false,
    disabled2: false,
    envId: 'cloud1-2gu4iyfmadd39493',
    gameId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    this.setData({
      userInfo: {
        "id": e.id,
        "img": e.img,
        "name": e.username,
        "leader": e.leader,
        "amount": e.amount
      }
    });
    this.getGame()
  },
  showGameToast() {
    this.setData({
      showModalStatus: !this.data.showModalStatus
    })
  },
  prevNum() {
    this.setData({
      number: this.data.number + 1
    });
  },
  nextNum() {
    this.setData({
      number: this.data.number - 1
    });
  },
  prevNum1() {
    this.setData({
      number1: this.data.number1 >= 10 ? 10 : this.data.number1 + 1,
      disabled1: this.data.number1 !== 0 ? false : true,
      disabled2: this.data.number1 !== 10 ? false : true
    });
  },
  nextNum1() {
    this.setData({
      number1: this.data.number1 <= 0 ? 0 : this.data.number1 - 1,
      disabled1: this.data.number1 !== 0 ? false : true,
      disabled2: this.data.number1 !== 10 ? false : true
    });
  },
  /**
   * 创建游戏
   */
  gameSubmit(e) {
    var that = this;
    var createTime = app.getNowDate();
    var addGameInfo = {
      "gameName": e.detail.value.gameName,
      "leaderId": that.data.userInfo.id,
      "gameImage": that.data.userInfo.img,
      "allowCount": e.detail.value.userCount,
      "createTime": createTime,
      "users": []
    };
    if(e.detail.value.gameName&&e.detail.value.userCount){
      wx.cloud.callFunction({
        name: 'homePlayFuntions',
        config: {
          env: that.data.envId
        },
        data: {
          type: 'addGame',
          game: addGameInfo
        }
      }).then((resp) => {
        that.getGame();
        that.showGameToast()
      }).catch((e) => {
        console.log(e);
      });
    }else{
      wx.showToast({
        icon: 'none',
        title: '请填写信息',
      })
    }

  },
  /**
   * 获取游戏列表信息
   */
  getGame() {
    var that = this;
    wx.cloud.callFunction({
      name: 'homePlayFuntions',
      config: {
        env: that.data.envId
      },
      data: {
        type: 'getGameInfo',
      }
    }).then((resp) => {
      that.setData({
        gameInfo: resp.result.data,
      })
    }).catch((e) => {
      console.log(e);
    });
  },
  /**
   * 成为群主
   */
  toBeLeader: function () {
    let that = this;
    let userId = that.data.userInfo.id;
    wx.cloud.callFunction({
      name: 'homePlayFuntions',
      config: {
        env: that.data.envId
      },
      data: {
        type: 'userTobeLeader',
        user: userId
      }
    }).then((resp) => {
      that.setData({
        ["userInfo.leader"]: 1
      })
    }).catch((e) => {
      console.log(e);
    });
  },
   /**
   * 加入游戏
   */
  joinGame(e) {
    var that = this;
    var gameId = e.target.dataset.id;
    var userId = that.data.userInfo.id;
    var userImg = that.data.userInfo.img;
    var userName = that.data.userInfo.name;
    var user = {"userId":userId,"userImg":userImg,"userName":userName}
    wx.cloud.callFunction({
      name: 'homePlayFuntions',
      config: {
        env: that.data.envId
      },
      data: {
        type: 'getGameUsers',
        game: gameId,
      }
    }).then((resp) => {
      var users = resp.result.data[0].users;
      users.push(user);
      wx.cloud.callFunction({
        name: 'homePlayFuntions',
        config: {
          env: that.data.envId
        },
        data: {
          type: 'joinGame',
          game: gameId,
          users: users
        }
      }).then((resp) => {
        if(resp.result.stats.updated==1){
          that.getGame();
          wx.navigateTo({
            url: '/pages/detail/detail' + '?game_id=' + gameId + '&user_id=' + userId
          });
        }else{
          wx.showToast({
            icon: 'none',
            title: "服务繁忙，请重试"
          });
        }
      }).catch((e) => {
        console.log(e);
      });
    }).catch((e) => {
      console.log(e);
    });
  },
   /**
   * 查看游戏详情
   */
  showGame(e) {
    var gameId = e.target.dataset.id;
    var userId = e.target.dataset.userid;
    wx.navigateTo({
      url: '/pages/detail/detail' + '?game_id=' + gameId + '&user_id=' + userId
    })
  }
})